package me.shurufa.widget.simpledialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.LinearLayout;
import android.widget.TextView;

import me.shurufa.R;
import me.shurufa.widget.colordialog.AnimationLoader;

/**
 * 作者 : andy
 * 日期 : 16/3/20 21:50
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 符合UI的SimpleDialog
 */
public class SimpleDialog extends Dialog implements View.OnClickListener {

    private View mDialogView;

    private TextView mTitleTv, mContentTv, mPositiveBtn, mNegativeBtn;

    private LinearLayout mDividerView;

    private AnimationSet mAnimIn, mAnimOut;

    private OnPositiveListener mPositiveListener;

    private OnNegativeListener mNegativeListener;

    private CharSequence mContentText, mPositiveText, mNegativeText;
    private String mTitleText;

    private boolean mIsShowAnim = true;
    private int mPositiveButtonVisibiliy = View.VISIBLE;
    private int mNegativeButtonVisibility = View.VISIBLE;
    private int mDividerVisibility = View.VISIBLE;
    private int mTitleVisibility = View.GONE;
    private int mContentGravity = Gravity.CENTER;

    public SimpleDialog(Context context) {
        this(context, 0);
    }

    public SimpleDialog(Context context, int theme) {
        super(context, R.style.simple_dialog);
        init();
    }

    private void callDismiss() {
        super.dismiss();
    }

    private void init() {
        mAnimIn = AnimationLoader.getInAnimation(getContext());
        mAnimOut = AnimationLoader.getOutAnimation(getContext());
        initAnimListener();
    }

    @Override
    public void setTitle(int titleId) {
        setTitle(getContext().getText(titleId));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View contentView = View.inflate(getContext(), R.layout.layout_simple_dialog, null);
        setContentView(contentView);

        mDialogView = getWindow().getDecorView().findViewById(android.R.id.content);
        mDividerView = (LinearLayout) contentView.findViewById(R.id.divider_verticle);
        mTitleTv = (TextView) contentView.findViewById(R.id.tvTitle);
        mContentTv = (TextView) contentView.findViewById(R.id.tvContent);

        mPositiveBtn = (TextView) contentView.findViewById(R.id.btnPositive);
        mNegativeBtn = (TextView) contentView.findViewById(R.id.btnNegative);


        mPositiveBtn.setOnClickListener(this);
        mNegativeBtn.setOnClickListener(this);

        mTitleTv.setText(mTitleText);
        mContentTv.setGravity(mContentGravity);
        mContentTv.setText(mContentText);
        mPositiveBtn.setText(mPositiveText);
        mNegativeBtn.setText(mNegativeText);

        mDividerView.setVisibility(mDividerVisibility);
        mPositiveBtn.setVisibility(mPositiveButtonVisibiliy);
        mNegativeBtn.setVisibility(mNegativeButtonVisibility);
        mTitleTv.setVisibility(mTitleVisibility);
    }

    @Override
    protected void onStart() {
        super.onStart();
        startWithAnimation(mIsShowAnim);
    }

    @Override
    public void dismiss() {
        dismissWithAnimation(mIsShowAnim);
    }

    private void startWithAnimation(boolean showInAnimation) {
        if (showInAnimation) {
            mDialogView.startAnimation(mAnimIn);
        }
    }

    private void dismissWithAnimation(boolean showOutAnimation) {
        if (showOutAnimation) {
            mDialogView.startAnimation(mAnimOut);
        } else {
            super.dismiss();
        }
    }

    private void initAnimListener() {
        mAnimOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mDialogView.post(new Runnable() {
                    @Override
                    public void run() {
                        callDismiss();
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (R.id.btnPositive == id) {
            mPositiveListener.onClick(this);
        } else if (R.id.btnNegative == id) {
            mNegativeListener.onClick(this);
        } else {
        }
    }

    public SimpleDialog setAnimationEnable(boolean enable) {
        mIsShowAnim = enable;
        return this;
    }

    public SimpleDialog setAnimationIn(AnimationSet animIn) {
        mAnimIn = animIn;
        return this;
    }

    public SimpleDialog setAnimationOut(AnimationSet animOut) {
        mAnimOut = animOut;
        initAnimListener();
        return this;
    }

    public SimpleDialog setOnPositiveListener(CharSequence text, OnPositiveListener l) {
        mPositiveText = text;
        mPositiveListener = l;
        return this;
    }

    public SimpleDialog setOnPositiveListener(int textId, OnPositiveListener l) {
        return setOnPositiveListener(getContext().getText(textId), l);
    }

    public SimpleDialog setOnNegativeListener(CharSequence text, OnNegativeListener l) {
        mNegativeText = text;
        mNegativeListener = l;
        return this;
    }

    public SimpleDialog setOnNegativeListener(int textId, OnNegativeListener l) {
        return setOnNegativeListener(getContext().getText(textId), l);
    }

    public SimpleDialog setContentText(CharSequence text) {
        mContentText = text;
        return this;
    }

    public SimpleDialog setContentTextGravity(int gravity) {
        mContentGravity = gravity;
        return this;
    }

    public SimpleDialog setContentText(int textId) {
        return setContentText(getContext().getText(textId));
    }

    public SimpleDialog setTitleText(String text) {
        mTitleText = text;
        return this;
    }

    public CharSequence getContentText() {
        return mContentText;
    }

    public CharSequence getPositiveText() {
        return mPositiveText;
    }

    public CharSequence getNegativeText() {
        return mNegativeText;
    }

    /**
     * 设置negative button visibility
     * @param visibility
     */
    public SimpleDialog setNegativeButtonVisibility(int visibility) {
        mDividerVisibility = visibility;
        mNegativeButtonVisibility = visibility;
        return this;
    }

    /**
     * 设置positive button visibility
     * @param visibility
     */
    public SimpleDialog setPositiveButtonVisibility(int visibility) {
        mDividerVisibility = visibility;
        mPositiveButtonVisibiliy = visibility;
        return this;
    }

    public SimpleDialog setTitleVisibility(int visibility) {
        mTitleVisibility = visibility;
        return this;
    }


    public interface OnPositiveListener {
        void onClick(SimpleDialog dialog);
    }

    public interface OnNegativeListener {
        void onClick(SimpleDialog dialog);
    }
}